<template>
  <div>
     <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
      <el-col :span="7" class="bd">
        <el-button type="primary" @click="ADDdialogVisible = true">添加分类</el-button>
      </el-col>
      </el-row>
      <el-row></el-row>
      <tree-table show-index border :data="productList" :columns="columns" :selection-type="false" :expand-type="false">
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.deleted === false" style="color:lightgreen"></i>
          <i class="el-icon-error" v-else style="color:red"></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.level == '0'">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.level == '1'">二级</el-tag>
          <el-tag type="info" v-else>三级</el-tag>
        </template>
        <template slot-scope="scope" slot="operate">
            <el-button v-model="scope.row.id" type="primary" icon="el-icon-edit" size="mini" >编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" >删除</el-button>
        </template>
      </tree-table>
    </el-card>
     <!-- 分页区域 -->
       <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
       </el-pagination>
       <!-- 添加分类按钮弹窗 -->
        <el-dialog
          title="添加分类"
          :visible.sync="ADDdialogVisible"
           width="40%"
           @close="addClassClose"
           >
           <!-- 添加信息 -->
          <el-form ref="addClassform" :model="addClassForm" :rules="classRules" label-width="80px">
            <el-form-item label="分类名称" prop="label">
              <el-input v-model="addClassForm.label" ></el-input>
            </el-form-item>
            <el-form-item label="父级分类">
               <el-cascader
                  v-model="selectKey"
                  :options="productList"
                  clearable
                  change-on-select
                  expand-trigger="hover"
                  @change="handleChange"
                  :props="addClassList">
               </el-cascader>
            </el-form-item>
          </el-form>
          <!-- 底部按钮 -->
          <span slot="footer" class="dialog-footer">
          <el-button @click="ADDdialogVisible = false">取 消</el-button>
          <el-button @click="addClass" type="primary" >确 定</el-button>
          </span>
        </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      productList: [],
      columns: [
        {
          label: '分类名称',
          prop: 'label'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operate'
        }
      ],
      queryInfo: {
        pagenum: 1,
        pagesize: 2
      },
      total: 0,
      ADDdialogVisible: false,
      addClassForm: {
        label: '',
        pid: 0,
        level: 0
      },
      classRules: {
        label: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      addClassList: {
        value: '_id',
        label: 'label',
        children: 'children'
      },
      selectKey: []
    }
  },
  created () {
    this.getProductList()
  },
  methods: {
    async getProductList () {
      const res = await this.$http.get('/product', { params: this.queryInfo })
      this.productList = res.data.list
      this.total = res.data.total
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getProductList()
    },
    handleCurrentChange (newSize) {
      this.queryInfo.pagenum = newSize
      this.getProductList()
    },
    handleChange () {
      if (this.selectKey.length > 0) {
        this.addClassForm.pid = this.selectKey[this.selectKey.length - 1]
        this.addClassForm.level = this.selectKey.length
      } else {
        this.addClassForm.pid = 0
        this.addClassForm.level = 0
      }
    },
    async addClass () {
      this.$refs.addClassform.validate(valid => {
        if (!valid) return this.$message.error('请输入合法字符！')
        this.ADDdialogVisible = false
        this.$message.success('修改信息成功！')
      })
    },
    addClassClose () {
      this.$refs.addClassform.resetFields()
      this.selectKey = []
      this.addClassForm.pid = 0
      this.addClassForm.level = 0
    }
  }
}
</script>
<style lang="less" scoped>
.bd {
  margin-bottom:20px;
}

.el-cascader {
  width:100%;
}
</style>
